define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zcorder/index' + location.search,
                    add_url: 'fas/zcorder/add',
                    edit_url: 'fas/zcorder/edit',
                    del_url: 'fas/zcorder/del',
                    multi_url: 'fas/zcorder/multi',
                    import_url: 'fas/zcorder/import',
                    table: 'fas_zcorder',
                }
            });

            var table = $("#table");

            $(".btn-add").data("area", ["100%", "100%"]);
            $(".btn-audit").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".btn-auditone").data("area", ["100%", "100%"]);
            });


            var buttons = [];
            if (Config.isdiao) {
                buttons.push({
                    name: 'op1',
                    title: __('调拨'),
                    classname: 'btn btn-xs btn-info btn-dialog',
                    icon: 'fa fa-truck',
                    url: 'fas/zcorder/diaobo'
                });
            }

            if (Config.iscg) {
                buttons.push({
                    name: 'op2',
                    title: __('采购'),
                    classname: 'btn btn-xs btn-warning btn-dialog',
                    icon: 'fa fa-cart-plus',
                    url: 'fas/zcorder/caigou'
                });
            }

            if (Config.isys) {
                buttons.push({
                    name: 'op3',
                    title: __('验收'),
                    classname: 'btn btn-xs btn-info btn-dialog',
                    icon: 'fa  fa-user-secret',
                    url: 'fas/zcorder/check'
                });
            }

            if (Config.ishenhe) {
                buttons.push({
                    name: 'op4',
                    title: __('终审'),
                    classname: 'btn btn-xs btn-warning btn-dialog',
                    icon: 'fa  fa-flag',
                    url: 'fas/zcorder/shenhe'
                });
            }

            buttons.push({
                name: 'op5',
                title: __('详情'),
                classname: 'btn btn-xs btn-primary btn-addtabs',
                icon: 'fa fa-list',
                url: 'fas/zcorder/detail'
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        //{checkbox: true},
                        {
                            field: 'operate',
                            width: "130px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'zctype.name', title: __('Zctype.name'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'num', title: __('Num'), operate:false},
                        {field: 'num0', title: __('Num0'), operate:false},
                        {field: 'num1', title: __('Num1'), operate:false},
                        {field: 'cgstate', title: __('Cgstate'), searchList: {"0":__('Cgstate 0'),"1":__('Cgstate 1'),"2":__('Cgstate 2')}, formatter: Table.api.formatter.normal},
                        {field: 'dbstate', title: __('Dbstate'), searchList: {"0":__('Dbstate 0'),"1":__('Dbstate 1'),"2":__('Dbstate 2'),"3":__('Dbstate 3')}, formatter: Table.api.formatter.normal},
                        {field: 'ysstate', title: __('Ysstate'), searchList: {"0":__('Ysstate 0'),"1":__('Ysstate 1')}, formatter: Table.api.formatter.normal},
                        {field: 'state', title: __('State'), defaultValue:"0", searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},

                        //{field: 'po', title: __('采购单'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        //{field: 'gys', title: __('Gys'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'fgroup', title: __('Fgroup'), operate:false},
                        {field: 'ffactory', title: __('Ffactory')},
                        {field: 'sqdw', title: __('Sqdw')},
                        {field: 'sydd', title: __('Sydd'), operate:false},
                        //{field: 'otype', title: __('Otype'), searchList: {"0":__('Otype 0'),"1":__('Otype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'rukudate', title: __('Rukudate'), operate:false},
                        {field: 'ysdate', title: __('Ysdate'), operate:false},
                        {field: 'gsdate', title: __('Gsdate'), operate:false},
                        {field: 'ysmanager', title: __('Ysmanager'), operate:false},
                        {field: 'cgmanager', title: __('Cgmanager'), operate:false},
                        {field: 'dbmanager', title: __('Dbmanager'), operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:false},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zctype.id', title: __('Zctype.id')},

                        //{field: 'zctype.xh', title: __('Zctype.xh')},
                        //{field: 'zctype.type', title: __('Zctype.type')},
                        //{field: 'zctype.zclh', title: __('Zctype.zclh')},
                        //{field: 'zctype.create_time', title: __('Zctype.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zctype.update_time', title: __('Zctype.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_fas_zcorder', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_fas_zcorder')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_fas_zcorder')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $("#c-zcdevice_ids").data("params", function (obj) {
                var zctype_id = $("#c-zctype_id").val();
                //console.log(zctype_id + " h11");
                return {custom: {zctype_id: zctype_id}};
            });

            $(document).on("change", "#c-zctype_id", function () {
                $('#c-zcdevice_ids').selectPageClear();
            });

            $(document).on("click", "#b-zctypename", function () {
                var url = 'fas/zcorder/zctypeselect';
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ["100%", "100%"],
                    callback:function(data){
                        console.log(data);
                        $("#c-zctype_id").val(data.id);
                        $("#c-zctypename").val(data.name);

                        $("#c-zcdevice_ids").val("");
                        $("#c-zcdevicenames").text("");
                        $("#c-num1").val("0");
                        updatenum0();
                    }
                };
                Fast.api.open(url, "资产设备型体列表", options);
            });

            $(document).on("click", "#b-zcdevicenames", function () {
                var zctype_id = $("#c-zctype_id").val();
                var url = 'fas/zcorder/zcdeviceselect/ids/' + zctype_id;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ["90%", "90%"],
                    callback:function(data){
                        console.log("222");
                        console.log(data);
                        $("#c-zcdevice_ids").val(data.ids);
                        $("#c-zcdevicenames").text(data.names);
                        $("#c-num1").val(data.num);
                        updatenum0();
                    }
                };
                Fast.api.open(url, "闲置设备列表", options);
            });

            $(document).on("change", "#c-num", function () {
                updatenum0();
            });

            function updatenum0() {
                var num = parseInt($('#c-num').val());
                var num1 = parseInt($('#c-num1').val());
                var num0 = num - num1;
                if (num0 >= 0) {
                    $('#c-num0').val(num0 + "");
                }
            }

            Controller.api.bindevent();
        },
        edit: function () {

            $("#c-zcdevice_ids").data("params", function (obj) {
                var zctype_id = $("#c-zctype_id").val();
                //console.log(zctype_id + " h11");
                return {custom: {zctype_id: zctype_id}};
            });

            $(document).on("change", "#c-zctype_id", function () {
                $('#c-zcdevice_ids').selectPageClear();
            });

            $(document).on("click", "#b-zctypename", function () {
                var url = 'fas/zcorder/zctypeselect';
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ["100%", "100%"],
                    callback:function(data){
                        console.log(data);
                        $("#c-zctype_id").val(data.id);
                        $("#c-zctypename").val(data.name);

                        $("#c-zcdevice_ids").val("");
                        $("#c-zcdevicenames").text("");
                        $("#c-num1").val("0");
                        updatenum0();
                    }
                };
                Fast.api.open(url, "资产设备型体列表", options);
            });

            $(document).on("click", "#b-zcdevicenames", function () {
                var zctype_id = $("#c-zctype_id").val();
                var url = 'fas/zcorder/zcdeviceselect/ids/' + zctype_id;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ["90%", "90%"],
                    callback:function(data){
                        console.log("222");
                        console.log(data);
                        $("#c-zcdevice_ids").val(data.ids);
                        $("#c-zcdevicenames").text(data.names);
                        $("#c-num1").val(data.num);
                        updatenum0();
                    }
                };
                Fast.api.open(url, "闲置设备列表", options);
            });

            $(document).on("change", "#c-num", function () {
                updatenum0();
            });

            function updatenum0() {
                var num = parseInt($('#c-num').val());
                var num1 = parseInt($('#c-num1').val());
                var num0 = num - num1;
                if (num0 >= 0) {
                    $('#c-num0').val(num0 + "");
                }
            }

            Controller.api.bindevent();
        },
        diaobo: function () {
            Controller.api.bindevent();
        },
        caigou: function () {

            Controller.api.bindevent();
        },
        check: function () {

            Controller.api.bindevent();
        },
        shenhe: function () {

            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        zctypeselect: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zcorder/zctypeselect',
                    add_url: 'fas/zctype/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'fas_zctypeselect',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: false,
                searchFormVisible: true,
                showExport: false,
                columns: [
                    [
                        {
                            field: 'buttons',
                            width: "60px",
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            operate:false,
                            buttons: [
                                {
                                    name: '选择',
                                    text: __('选择'),
                                    title: __('选择资产设备型号'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'fas/zcorder/getzctypeselect',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回數據：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，則必須使用return false;
                                        Fast.api.close(data);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'name', title: __('Zctype.name'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'xh', title: __('Zctype.xh'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'type', title: __('Zctype.type'), searchList: {"成型设备":__('成型设备'),"线外加工设备":__('线外加工设备'),"组底设备":__('组底设备'),"裁断设备":__('裁断设备'),"针车设备":__('针车设备'),"其他设备":__('其他设备')}, formatter: Table.api.formatter.normal},
                        {field: 'zclh', title: __('Zctype.zclh')},
                    ]
                ]
            });


            // 為表格綁定事件
            Table.api.bindevent(table);

        },
        zcdeviceselect: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zcorder/zcdeviceselect',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'fas/zcdevice/multi',
                    table: 'fas_zcdeviceselect',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'zctype.name', title: __('Zctype.name'), operate:false},
                        {field: 'zctype.xh', title: __('Zctype.xh'), operate:false},
                        //{field: 'fzr', title: __('负责人'), operate:false},
                        {field: 'fgroup', title: __('Fgroup'), operate: 'LIKE %...%', operate:false},
                        {field: 'ffactory', title: __('Ffactory'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'sydw', title: __('使用单位'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'sydd', title: __('Sydd'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'zctype.type', title: __('Zctype.type'), searchList: {"成型设备":__('成型设备'),"线外加工设备":__('线外加工设备'),"组底设备":__('组底设备'),"裁断设备":__('裁断设备'),"针车设备":__('针车设备'),"其他设备":__('其他设备')}},
                        //{field: 'zctype.zclh', title: __('Zctype.zclh'), operate:false},
                        //{field: 'qydate', title: __('Qydate'), operate:false},
                        //{field: 'sbsynx', title: __('Sbsynx'), operate:false},
                        //{field: 'sbjz', title: __('Sbjz'), operate:false},
                        //{field: 'cz', title: __('Cz'), operate:false},
                    ]
                ]
            });

            // 批量加入
            $(document).on("click", ".btn-pljr", function () {
                var ids = Table.api.selectedids(table);
                var url = 'fas/zcorder/getdeviceselect?ids=' + ids;

                var that = this;
                Layer.confirm(__('确定选择这些闲置设备?'), {icon: 3, title: '提示'}, function (index) {
                    Backend.api.ajax({
                        url: url,
                    },function (data, ret) {
                        console.log("111");
                        console.log(ret);
                        Layer.close(index);
                        Fast.api.close(data);
                    }, function(data, ret){
                        //失败的回调
                        return false;
                    });
                });
            });

            // 為表格綁定事件
            Table.api.bindevent(table);

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});